package com.danger.Activity.Fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.danger.Activity.Adapter.MyAdapter;
import com.danger.Activity.Bean.Course;
import com.danger.Activity.Bean.Result;
import com.danger.Activity.MainActivity;
import com.danger.Activity.R;
import com.danger.Activity.Util.GsonJson;
import com.danger.Activity.Util.LoadJson;
import com.danger.Activity.Util.OnLoadJsonListener;

import java.util.List;

/**
 * Created by 俊 on 2016/5/14.
 */
public class MainFragment extends BackHandledFragment {

    private boolean hadIntercept;
    private View view;
    private RecyclerView recyclerView;
    private Toolbar toolbar;
    private TextView toolbar_title;
    private MainActivity activity;
    private String url = "http://www.imooc.com/api/teacher?type=4&num=30";
    private MyAdapter adapter;
    private List<Course> list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (view == null) {
            view = inflater.inflate(R.layout.fragment_main, container, false);
        }

        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }

        initView();
        initEvent();

        return view;
    }

    private void initView() {
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        activity = (MainActivity) getActivity();
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        toolbar.setTitle("");
        activity.setSupportActionBar(toolbar);
        toolbar_title = (TextView) view.findViewById(R.id.toolbar_title);
        toolbar_title.setText("首页");
    }

    private void initEvent() {
        LoadJson loadJson = new LoadJson(activity);
        loadJson.getJSONByGet(url, new OnLoadJsonListener() {
            @Override
            public void onSuccess(String result) {
                Log.i("TAG", "result = " + result);
                Result result1 = GsonJson.parseJson(result, Result.class);

                Log.i("TAG", result1.getData().get(0).getDescription());

                if (result1.getStatus() == 1) {
                    list = result1.getData();
                    adapter = new MyAdapter(list, activity);
                    adapter.setOnItemClickLitener(new MyAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            DetailFragment fragment = new DetailFragment();
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("object", list.get(position));
                            fragment.setArguments(bundle);
                            getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.container, fragment).commit();
                        }

                        @Override
                        public void onItemLongClick(View view, int position) {

                        }
                    });
                    recyclerView.setAdapter(adapter);
                    recyclerView.setLayoutManager(new LinearLayoutManager(activity));
                } else {
                    Toast.makeText(activity, "出错", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(String error) {
                Toast.makeText(activity, "出错" + error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }
}
